package com.talon.ali_player;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.aliyun.player.AliListPlayer;
import com.aliyun.player.AliPlayer;
import com.aliyun.player.AliPlayerFactory;
import com.aliyun.player.IPlayer;
import com.aliyun.player.bean.ErrorCode;
import com.aliyun.player.bean.ErrorInfo;
import com.aliyun.player.bean.InfoBean;
import com.aliyun.player.bean.InfoCode;
import com.aliyun.player.source.UrlSource;
import com.talon.lib_base.ConfigApi;

public class AliPlayerActivity extends AppCompatActivity {
    AliListPlayer aliPlayer;
    ConstraintLayout rootView;
    SurfaceView surfaceView;
    String url = ConfigApi.videoUrl;
    private String TAG = "阿里播放器~~";
    private long time = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ali_player);
        surfaceView = findViewById(R.id.surfaceView);
        rootView = findViewById(R.id.rootView);
        aliPlayer = AliPlayerFactory.createAliListPlayer(this);
        aliPlayer.setOnErrorListener(new IPlayer.OnErrorListener() {
            //此回调会在使用播放器的过程中，出现了任何错误，都会回调此接口。

            @Override
            public void onError(ErrorInfo errorInfo) {
                ErrorCode errorCode = errorInfo.getCode(); //错误码
                String errorMsg = errorInfo.getMsg(); //错误描述
                //出错后需要停止掉播放器
                aliPlayer.stop();
            }
        });
        aliPlayer.setOnPreparedListener(new IPlayer.OnPreparedListener() {
            // 调用aliPlayer.prepare()方法后，播放器开始读取并解析数据。成功后，会回调此接口。

            @Override
            public void onPrepared() {
                //一般调用start开始播放视频
                float result = System.currentTimeMillis() - time;
                Log.i("xiaote", TAG + "onPrepared time :" + result);
                aliPlayer.start();
            }
        });
        aliPlayer.setOnCompletionListener(new IPlayer.OnCompletionListener() {
            //播放完成之后，就会回调到此接口。
            @Override
            public void onCompletion() {
                //一般调用stop停止播放视频
                aliPlayer.stop();
            }
        });
        aliPlayer.setOnInfoListener(new IPlayer.OnInfoListener() {
            //播放器中的一些信息，包括：当前进度、缓存位置等等
            @Override
            public void onInfo(InfoBean infoBean) {
                InfoCode code = infoBean.getCode(); //信息码
                String msg = infoBean.getExtraMsg();//信息内容
                long value = infoBean.getExtraValue(); //信息值

                //当前进度：InfoCode.CurrentPosition
                //当前缓存位置：InfoCode.BufferedPosition
            }
        });
        aliPlayer.setOnLoadingStatusListener(new IPlayer.OnLoadingStatusListener() {
            //播放器的加载状态, 网络不佳时，用于展示加载画面。

            @Override
            public void onLoadingBegin() {
                //开始加载。画面和声音不足以播放。
                //一般在此处显示圆形加载
            }

            @Override
            public void onLoadingProgress(int percent, float netSpeed) {
                //加载进度。百分比和网速。
            }

            @Override
            public void onLoadingEnd() {
                //结束加载。画面和声音可以播放。
                //一般在此处隐藏圆形加载
            }
        });
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                aliPlayer.setSurface(holder.getSurface());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                aliPlayer.surfaceChanged();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                aliPlayer.setSurface(null);
            }
        });

        UrlSource urlSource = new UrlSource();
        urlSource.setUri(url);
        aliPlayer.setScaleMode(IPlayer.ScaleMode.SCALE_TO_FILL);
        aliPlayer.setDataSource(urlSource);
        time = System.currentTimeMillis();
        aliPlayer.prepare();
    }

    @Override
    protected void onStop() {
        super.onStop();
        aliPlayer.release();
    }
}